# -*- coding: UTF-8 -*- #
"""
@filename:01-2保存视频.py
@author:JiYujia
@time:2022-09-04
"""
import cv2

# 本地视频
cap = cv2.VideoCapture('pics/dog.mp4')

# 摄像头
# cap = cv2.VideoCapture('0')

# 读取网络视频
# cap = cv2.VideoCapture('https://bkrc.oss-cn-beijing.aliyuncs.com/videos/7b380bb1e47623338922e61dfc93d69e.mp4')

if not cap.isOpened():
    print('ERROR')

fourcc =cv2.VideoWriter_fourcc(*'MJPG')

# 保存视频
# print('cap3:', cap.get(3))
# print('cap4:', cap.get(4))

frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_heigh = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print('width:', frame_width, 'heigh:', frame_heigh)

out = cv2.VideoWriter('pics/test_save2.avi', fourcc, 30, (frame_width, frame_heigh))

# 出问题：视频写入的图像尺寸与画布尺寸不对应

# 仅保存
while cap.isOpened():
    state, frame = cap.read()
    if state == True:
        out.write(frame)
    else:
        break

# 保存并播放
# while cap.isOpened():
#     state, frame = cap.read()
#     if state == True:
#         out.write(frame)
#         cv2.imshow('frame', frame)
#         if cv2.waitKey(1) & 0xFF == ord('q'):
#             break
#     else:
#         break

cap.release()
out.release()
cv2.destroyAllWindows()
# 如果之前没有释放掉内存的操作的话destroyallWIndows会释放掉被那个变量占用的内存

# 参考修改代码：https://blog.csdn.net/weixin_48994268/article/details/114458138
